package com.ruoyi.web.controller.data;


import com.huoju.common.utils.DateUtils;
import com.huoju.common.utils.OperateDateUtils;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.system.service.IMUserMachineService;
import com.ruoyi.system.service.IUUserService;
import com.ruoyi.system.service.IWRecordRechargeService;
import com.ruoyi.system.service.IWRecordTransferInService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.crypto.Data;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


/**
 * 获取首页数据
 * @author 马运动
 */


@RestController
@RequestMapping("/data/config")
public class DataController {

    @Autowired
    private IUUserService userService;

    @Autowired
    private IMUserMachineService imUserMachineService;

    @Autowired
    private IWRecordRechargeService iwRecordRechargeService;

    @Autowired
    private IWRecordTransferInService iwRecordTransferInService;



    /**
     * 上方四个统计
     * @return
     */

    @GetMapping(value = "/getUserCount")
    public AjaxResult test()
    {

        Map<String,Object> map = new HashMap(4);
        //用户量
        map.put("userCount",userService.selectUserTotal(null));

       //查询矿机每日购买金额
        map.put("messageCount",imUserMachineService.selectMUserMachineTotal(null));

//       查询每日充值金额
        map.put("orderCount",iwRecordRechargeService.selectWRecordRechargeTotal(null));

        //充值总额度
        map.put("priceCount",iwRecordTransferInService.selectWRecordTransferInTotal(null));
        return AjaxResult.success(map);
    }


    /**
     * 走势图
     * @return
     */

    @GetMapping(value = "/statusic")
    public AjaxResult test1()
    {
        Map<String,Object> map = new HashMap(6);

        List<Integer> list = new ArrayList<>();


        List<Integer> list1 = new ArrayList<>();


        List<Integer> list2 = new ArrayList<>();


        List<Integer> list3 = new ArrayList<>();


        String start = "";
        String end = "";
        try {

            LocalDateTime time1 = LocalDateTime.now().plusDays(-6);
            DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            start = dtf1.format(time1);
            end = DateUtils.todayLast();
        }catch (Exception e){
            e.printStackTrace();
        }

        List<String> daysStr = OperateDateUtils.findDaysStr(start, end);

        if (!daysStr.isEmpty()){
            daysStr.forEach(d -> {
                //开始时间
                String createTime = d;
                //用户量
                list.add(userService.selectUserTotal(createTime));

                //查询矿机每日购买金额
                list1.add(imUserMachineService.selectMUserMachineTotal(createTime));

//                查询每日充值金额
                list2.add(iwRecordRechargeService.selectWRecordRechargeTotal(createTime));

//                //充值额度
                list3.add(iwRecordTransferInService.selectWRecordTransferInTotal(createTime));

            });
        }


        map.put("userCount",list);
        map.put("messageCount",list1);
        map.put("orderCount",list2);
        map.put("priceCount",list3);

        String[] date = new String[daysStr.size()];
        daysStr.toArray(date);
        map.put("dateDate",date);

        return AjaxResult.success(map);
    }


    /**
     * 最近7天
     * @return
     */

    @GetMapping(value = "/getDate")
    public AjaxResult test2()
    {
        Map map = new HashMap();

        String start = null;
        String end = null;
        try {

            LocalDateTime time1 = LocalDateTime.now().plusDays(-6);
            DateTimeFormatter dtf1 = DateTimeFormatter.ofPattern("yyyy-MM-dd");
            start = dtf1.format(time1);
            end = DateUtils.todayLast();
        }catch (Exception e){
            e.printStackTrace();
        }
        List<String> daysStr = OperateDateUtils.findDaysStr(start, end);
        String[] date = new String[daysStr.size()];
        daysStr.toArray(date);


        map.put("d1",date[0].substring(5));
        map.put("d2",date[1].substring(5));
        map.put("d3",date[2].substring(5));
        map.put("d4",date[3].substring(5));
        map.put("d5",date[4].substring(5));
        map.put("d6",date[5].substring(5));
        map.put("d7",date[6].substring(5));

        return AjaxResult.success(map);
    }


}

